package one.util;

import java.io.File;
import java.io.StringWriter;
import java.util.Date;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.tools.generic.DateTool;
import org.apache.velocity.tools.generic.MathTool;
import org.apache.velocity.tools.generic.NumberTool;

/**
 * Velocity工具类
 * @Author      : ydh
 * @Date        : Jan 7, 2015
 * @Email       : ydh110100@163.com
 * @Version     : V1.0
 */
public class VelocityUtils {

	public static VelocityEngine getEngine(){
		VelocityEngine engine = new VelocityEngine();
		 Properties properties = new Properties();
		 properties.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");
		 properties.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
		 properties.setProperty(Velocity.PARSE_DIRECTIVE_MAXDEPTH, "8");
		 //properties.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, "");
		 engine.init(properties);
		 return engine;
	}
	
	/**
	 * 合并模版,返回合并后的字符串
	 * @param vm
	 * @param map
	 * @return
	 * @throws Exception
	 */
	public static String mergeToString(String vm, Map<String, Object> map) throws Exception{
		 VelocityEngine engine = getEngine();
		 // 封装几个默认值
		 map.put("date", new Date());
		 VelocityContext context = new VelocityContext(map);
		 context.put("dateTool", new DateTool());
		 context.put("mathTool", new MathTool());
		 context.put("stringUtils", new StringUtils());
		 context.put("numberTool", new NumberTool());
		 
		 StringWriter writer = new StringWriter();
		 engine.evaluate(context, writer, "", vm);
		 String result = writer.toString();
		 
		 return result;
	}
	
	/**
	 * 合并模版,并直接输出成文件
	 * @param vmPath 模版路径
	 * @param context 填充的内容
	 * @param filePath 生成文件路径
	 * @throws Exception
	 */
	public static void mergeToFile(String vmPath, Map<String, Object> context, String filePath, boolean append) throws Exception{
		String result = mergeToString(vmPath, context);
       File outFile = new File(filePath);
       FileUtils.writeStringToFile ( outFile, result, "UTF-8" , append );
       System.out.println(filePath + "\t---->\tOK!\n");
	}
	
	/**
	 * 合并模版,并直接输出成文件
	 * @param context 填充的内容
	 * @param filePath 生成文件路径
	 * @throws Exception
	 */
	public static void mergeStrToFile(String vm, Map<String, Object> context, String filePath, boolean append) throws Exception{
		String result = mergeToString(vm, context);
       File outFile = new File(filePath);
       FileUtils.writeStringToFile ( outFile, result, "UTF-8" , append );
       System.out.println(filePath + "\t---->\tOK!\n");
	}
	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		
		// TODO Auto-generated method stub
//		Map map = new HashMap();
//		map.put("say", "Hello,world!");
//		VelocityUtils va = new VelocityUtils();
//		va.mergeToFile("D:\\test\\haha.vm", map, "D:\\hi.txt", false);
		NumberTool numberTool = new NumberTool();
		System.out.println(numberTool.format("##.##%", 0.201));
	}

}
